// pages/main/main.js 主页Action
import { base64src } from "../../utils/base64src.js";
import http from "../../utils/baseApi.js";

Page({
	// 页面的初始数据
	data: {
		userInfo:null,
		strUserId:"",
		strAvatar:"",
		strNickname:"请登录",
		strUserStyle:"普通用户",
		strQrcodeBg:"https://api.zetikeji.com/beijin.jpg",
		strQrcode:"",
		strQrcodePath:"",
		isQrcodeShow:false,
		strFromMobile:"",
		isProvinceAgent:false,
		strFromContent:"",
		forDoctorOrSales:"sales",
		strDoctorQrcodeUrl:""
	},
	// 生命周期函数--监听页面加载
	onLoad: function (options) {
		let that = this;
		if (typeof(options.mobile) != "undefined") {
			// 通过分享打开
			if (options.mobile.length == 11) {
				wx.setStorageSync("strFromMobile",options.mobile);
			}
		}
		if (typeof(options.scene) != "undefined") {
			// 通过扫码打开
			let scene = decodeURIComponent(options.scene);
			let sArr = scene.split("=");
			if (sArr.length == 2) {
				wx.setStorageSync("strFromMobile",sArr[1]);
			}
		}
	},
	// 生命周期函数--监听页面显示
	onShow: function () {
		if (wx.getStorageSync("userInfo")) {
			let userInfo = wx.getStorageSync("userInfo");
			if (userInfo.isDeleted) {
				wx.clearStorageSync();
				return false;
			}
		}
		this.onGetUserInfo();
		let that = this;
		let timer = setTimeout(()=>{
			clearTimeout(timer);
			that.onBuildTransition();
		}, 1000);
	},
	// 绑定推介人关系
	onBuildTransition: function () {
		let that = this;
		let strFromMobile = wx.getStorageSync("strFromMobile");
		let strCurrentMobile = wx.getStorageSync("cellphone") ? wx.getStorageSync("cellphone") : "";
		if (strFromMobile.length > 0 && strCurrentMobile.length > 0) {
			http.post("wechat/recommend/relation",{
				strFromMobile:strFromMobile,
				strCurrentMobile:strCurrentMobile
			}).then((res)=>{
				console.log(res);
			});
		}
	},
	// 前往认证中心
	onTurn2identity: function () {
		if ( ! wx.getStorageSync("userInfo")) {
			wx.showModal({ title:"注意", content:"进入认证中心前, 请先授权登录!", showCancel:true, confirmText:"去登录", success:(res)=>{
				if (res.confirm) {
					wx.redirectTo({ url:"/pages/index/index" });
				}
			} })
		} else {
			let userInfo = wx.getStorageSync("userInfo");
			if (userInfo.isAgent && ! userInfo.isAgentDeleted) {
				wx.navigateTo({ url:"/pages/worker/worker" });
			} else {
				wx.navigateTo({ url:"/pages/identification/identification" });
			}
		}
	},
	// 前往个人资料
	onTurn2Info: function () {
		if ( ! wx.getStorageSync("userInfo")) {
			wx.showModal({ title:"注意", content:"显示或修改个人资料, 请先授权登录!", showCancel:true, confirmText:"去登录", success:(res)=>{
				if (res.confirm) {
					wx.redirectTo({ url:"/pages/index/index" });
				}
			} })
		} else {
			wx.navigateTo({ url:"/pages/personal/personal" });
		}
	},
	// 获取用户信息,通过strUserId
	onGetUserInfo: function () {
		let that = this;
		let strUserId = wx.getStorageSync("strUserId");
		http.post("wechat/information/info",{
			strUserId:strUserId
		}).then((res)=>{
			if (res.code == 2000) {
				wx.setStorageSync("userInfo",res.data);
				// add
				let datUserInfo = wx.getStorageSync("userInfo");
				that.setData({ userInfo:datUserInfo });
				if (datUserInfo != null && datUserInfo != "") {
					let strUsername = (datUserInfo.strRealName == "") ? "用户"+datUserInfo.intUserId : datUserInfo.strRealName;
					let strAvatar = wx.getStorageSync("strUserAvatar");
					let strUserStyle = datUserInfo.isVerify ? "推广业务员" : "普通用户(可完善资料成为业务员)";

					// 判断省级代理
					if (datUserInfo.isAgent && ! datUserInfo.isAgentDeleted) {
						strUserStyle = "推广业务员(省级代理)";
						that.setData({ isProvinceAgent:true });
					}
					that.setData({ strUserId:datUserInfo.intUserId, strAvatar:strAvatar, strNickname:strUsername, strUserStyle:strUserStyle });
				}
			}
		});
	},
	// 退出登录
	onLogout: function () {
		wx.clearStorageSync();
		wx.showToast({ title:"已成功登出!", icon:"success", duration:2000, mask:true });
		let that = this;
		let timer = setTimeout(()=>{
			clearTimeout(timer);
			wx.redirectTo({ url:"/pages/index/index" });
		},2000);
	},
	// 显示二维码浮窗
	onShowQrcodeCover: function () {
		this.onGetQrcode(wx.getStorageSync("cellphone"));
		this.setData({ isQrcodeShow:true,forDoctorOrSales:"sales" });
	},
	// 显示推广app二维码
	onShowDoctorQrcode: function () {
		this.onGenAppQrcode(wx.getStorageSync("cellphone"));
		this.setData({ isQrcodeShow:true,forDoctorOrSales:"doctor" });
	},
	// 隐藏二维码浮窗
	onHideQrcodeCover: function () {
		this.setData({ isQrcodeShow:false });
	},
	// 获取自己的二维码
	onGetQrcode: function (mobile) {
		let that = this;
		http.get("sale/get/wechat-token").then((res)=>{
			if (res.code == 2000) {
				let token = res.data.token;
				http.post("/sale/get/custom-qrcode",{
					strToken:token,
					strMobile:mobile
				}).then((res)=>{
					that.setData({ strQrcode:res.message });
				});
			}
		});
	},
	// 生成推广app二维码
	onGenAppQrcode: function (mobile) {
		let that = this;
		let strQrcodePath = wx.env.USER_DATA_PATH+"/qrcode.png";
		wx.request({
			url:"https://api.zetikeji.com/api/sale/doctor/qrcode",
			method:"get",
			data:{ strMobile:mobile },
			responseType:"arraybuffer",
			success:(res)=>{
				let data = res.data;
				that.setData({ strQrcode:"data:image/png;base64,"+wx.arrayBufferToBase64(data)});
				// 保存到临时文件
				let fs = wx.getFileSystemManager();
				fs.writeFileSync(strQrcodePath,that.data.strQrcode,"utf-8");
				wx.saveFile({
					tempFilePath:strQrcodePath,
					success:(res)=>{
						console.log(res.savedFilePath);
						that.setData({ strDoctorQrcodeUrl:res.savedFilePath });
					}
				});
			}
		});
	},
	// 分享传参数
	onShareAppMessage: function () {
		return {
			title: "您朋友邀请您加入成为业务员",
			desc: "邀请内容",
			path: "/pages/main/main?mobile="+wx.getStorageSync("cellphone")
		};
	},
	// 推荐医生时传图片
	onCopyQrcode2List: function () {
		if (wx.getStorageSync("cellphone")) {
			wx.downloadFile({
				url:"https://api.zetikeji.com/api/sale/doctor/qrcode?strMobile="+wx.getStorageSync("cellphone"),
				success:(res)=>{
					wx.showShareImageMenu({
						path: res.tempFilePath
					  });
				}
			});
		} else {
			wx.showModal({ title:"授权登录", content:"推荐医生朋友的功能需要授权登录启用, 请先登录!", showCancel:false });
		}
	}
})